<template>
    <div class="about">
        <h1>环信获取token页面</h1>
        <h2>获取的Token：{{Token}}</h2>
        <h2>Token过期时长：{{expires_time}}</h2>
        <h2>当前时间：{{date_now}}</h2>
        <h2>当前时间戳：{{unix_time_now}}</h2>
        <h2>Token过期时间：{{date_time_end}}</h2>
    </div>
</template>
<script>
    import {
        getToken1111//获取token接口
    } from '../api/im/imapi';
    export default {
        data() {
            return {
                Token : '',
                expires_time: '',
                unix_time_now: '',
                date_now: '',
                date_time_end: ''
            }
        },
        mounted() {
            this.getToken()
        },
        methods: {
            getToken(){
                //获得token
                return getToken1111().then((res) => {
                    //console.log(res)
                    if(res.status == 200){//正常返回
                        this.Token = res.data.access_token
                        this.expires_time = res.data.expires_in
                        this.unix_time_now = Date.parse(new Date())
                        this.date_now = this.format(Date.parse(new Date()))
                        this.date_time_end = this.format(this.unix_time_now + this.expires_time)
                    }else{
                        if(res.status == 401) {
                            console.log('Token过期了')
                        }else if(res.status == 400){
                            console.log('client_id 或 client_secret 错误')
                        }else if(res.status == 429 || res.status == 503){
                            console.log('接口被限流了')
                        }else{
                            console.log('其它问题找客服')
                            console.log('错误代码：' + res.status)
                            console.log('错误类型：' + res.data.error)
                            console.log('错误描述：' + res.data.error_description)
                        }
                    }
                }).catch(err => {
                    console.log(err)
                })
            },
            add0(m){
                return m<10?'0'+m:m
            },
            format(shijianchuo)
            {
                //shijianchuo是整数，否则要parseInt转换
                var time = new Date(shijianchuo);
                var y = time.getFullYear();
                var m = time.getMonth()+1;
                var d = time.getDate();
                var h = time.getHours();
                var mm = time.getMinutes();
                var s = time.getSeconds();
                return y+'-'+this.add0(m)+'-'+this.add0(d)+' '+this.add0(h)+':'+this.add0(mm)+':'+this.add0(s);
            }
        }

    }
</script>